import pygame, sys, os
from pygame.locals import *
from datamap import *
from level import *
from ai import gameAI
from driver import KeySignal

class Control(object):
    WINDOW = (640, 640)
    CAPTION = 'Mini Tank'
    FPS = 50
    def __init__(self):
        if sys.platform in ("win32", "win64"):
            os.environ["SDL_VIDEO_CENTERED"] = "1"
        pygame.init()
        self.__window = pygame.display.set_mode(Control.WINDOW)
        pygame.display.set_caption(Control.CAPTION)
        self.__clock = pygame.time.Clock()
        self.__level = 1
        self.__keys = [K_UP, K_DOWN, K_LEFT, K_RIGHT, K_SPACE]
    
    def start(self):
        # program loop
        while 1:
            # init map
            lvlmap = DataMap(mapcfg[self.__level])
            # game loop
            while 1:
                # keep fps
                self.__clock.tick(Control.FPS)
                # sys
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
                # get key
                key = pygame.key.get_pressed()
                for keyname in self.__keys:
                    if key[keyname]:
                        for spr in lvlmap.player:
                            spr.driver.send(KeySignal(keyname))
                # sprite update
                for spr in lvlmap.updates:
                    spr.update()
                # simple game state judge
                if self.game_judge(lvlmap): break
                # draw window
                self.__window.fill((0, 0, 0))
                lvlmap.render.draw(self.__window)
                pygame.display.update()
    
    def game_judge(self, lvlmap): # temp function
        over = 0 # 0: gaming, 1:game over, 2:game win
        str = ""
        for spr in lvlmap.player.sprites() + lvlmap.base.sprites():
            if not spr.vtl_alive(): 
                over = 1
                str = 'Game Over'
                break
        if over == 0 and len(lvlmap.enemy.sprites()) == 0:
            over = 2
            str = 'Game Win'
        if over != 0:
            font = pygame.font.SysFont('sans', 100, 1)
            fimage = font.render(str, 1, (100, 255, 100))
            pos = ((self.__window.get_rect().width - fimage.get_rect().width) / 2, 
                   (self.__window.get_rect().height - fimage.get_rect().height) / 2)
            while 1:
                self.__clock.tick(Control.FPS)
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        sys.exit()
                key = pygame.key.get_pressed()
                if key[K_RETURN]: break
                self.__window.fill((0, 0, 0))
                lvlmap.render.draw(self.__window)
                self.__window.blit(fimage, pos)
                pygame.display.update()
        return over
    
    
    
    
    
    
